package com.ymatou.iapi.deviceid.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.deviceid.parameter.GetEarliestActiveTimeBean;
import com.ymatou.iapi.deviceid.parameter.SaveBean;
import com.ymatou.iapi.deviceid.service.GetEarliestActiveTimeCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DeviceidCallService;
import com.ymttest.business.service.JUserCallService;

/**
*获取用户设备的最早激活时间
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetEarliestActiveTime{
	private static GetEarliestActiveTimeBean getearliestactivetimeBean;
	private static GetEarliestActiveTimeCall getearliestactivetimeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户设备的最早激活时间");
	}

	@Before
	public void caseUp() {
		getearliestactivetimeBean=new GetEarliestActiveTimeBean();
		getearliestactivetimeCall=new GetEarliestActiveTimeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getearliestactivetimeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetEarliestActiveTime_001() {
		Logger.start(true,"获取用户设备的最早激活时间成功");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userid = uc.getUserid();
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveBean.setUserid(userid);
			DeviceidCallService.saveCall(saveBean);
			getearliestactivetimeBean.setUserIds(Arrays.asList(userid));
			getearliestactivetimeCall.setData(getearliestactivetimeBean);
			getearliestactivetimeCall.callService();			
			Logger.verifyEquals("0", getearliestactivetimeCall.getString("code"), "验证code");
			JsonArray ja = getearliestactivetimeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(getearliestactivetimeBean.getUserIds().size(), ja.size(), "验证记录条数");
			ja.forEach(tinfo->{
				Integer tuid = tinfo.getAsJsonObject().get("userId").getAsInt();
				String activeTime = tinfo.getAsJsonObject().get("activeTime").getAsString();
				Logger.verifyEquals(true, getearliestactivetimeBean.getUserIds().contains(tuid), "验证uid");
				Logger.verifyNotNull(activeTime, "验证activeTime");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetEarliestActiveTime_002() {
		Logger.start(true,"不输userIds失败");
		try {
			getearliestactivetimeCall.setData(getearliestactivetimeBean);
			getearliestactivetimeCall.callService();			
			Logger.verifyEquals("100", getearliestactivetimeCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetEarliestActiveTime_003() {
		Logger.start(true,"输入userIds错误失败");
		try {
			//错误的id返回空的data list
			getearliestactivetimeBean.setUserIds(Arrays.asList(-10));
			getearliestactivetimeCall.setData(getearliestactivetimeBean);
			getearliestactivetimeCall.callService();			
			getearliestactivetimeCall.setData(getearliestactivetimeBean);
			getearliestactivetimeCall.callService();			
			Logger.verifyEquals("0", getearliestactivetimeCall.getString("code"), "验证code");
			JsonArray ja = getearliestactivetimeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(0, ja.size(), "验证记录条数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetEarliestActiveTime_004() {
		Logger.start(true,"获取多个用户的用户设备的最早激活时间成功,有多条记录");
		try {
			List<Integer> uids=new ArrayList<>();
			for (int i = 0; i < 5; i++) {
				UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
				UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
				Integer userid = uc.getUserid();
				SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
				saveBean.setUserid(userid);
				DeviceidCallService.saveCall(saveBean);
				uids.add(userid);
			}
			getearliestactivetimeBean.setUserIds(uids);
			getearliestactivetimeCall.setData(getearliestactivetimeBean);
			getearliestactivetimeCall.callService();			
			Logger.verifyEquals("0", getearliestactivetimeCall.getString("code"), "验证code");
			JsonArray ja = getearliestactivetimeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(getearliestactivetimeBean.getUserIds().size(), ja.size(), "验证记录条数");
			ja.forEach(tinfo->{
				Integer tuid = tinfo.getAsJsonObject().get("userId").getAsInt();
				String activeTime = tinfo.getAsJsonObject().get("activeTime").getAsString();
				Logger.verifyEquals(true, getearliestactivetimeBean.getUserIds().contains(tuid), "验证uid");
				Logger.verifyNotNull(activeTime, "验证activeTime");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetEarliestActiveTime_005() {
		Logger.start(true,"获取用户设备的最早激活时间成功,用户有多个设备激活时间");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userid = uc.getUserid();
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveBean.setUserid(userid);
			DeviceidCallService.saveCall(saveBean);
			getearliestactivetimeBean.setUserIds(Arrays.asList(userid));
			getearliestactivetimeCall.setData(getearliestactivetimeBean);
			getearliestactivetimeCall.callService();			
			Logger.verifyEquals("0", getearliestactivetimeCall.getString("code"), "验证code");
			JsonArray ja = getearliestactivetimeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(getearliestactivetimeBean.getUserIds().size(), ja.size(), "验证记录条数");
			JsonElement tinfo = ja.get(0);
			Integer tuid = tinfo.getAsJsonObject().get("userId").getAsInt();
			String activeTime = tinfo.getAsJsonObject().get("activeTime").getAsString();
			Logger.verifyEquals(true, getearliestactivetimeBean.getUserIds().contains(tuid), "验证uid");
			Logger.verifyNotNull(activeTime, "验证activeTime");
			Thread.sleep(2000);
			SaveBean saveBean2 = DeviceidCallService.setDefaultFullSaveBean();
			saveBean2.setUserid(userid);
			DeviceidCallService.saveCall(saveBean2);
			getearliestactivetimeCall.setData(getearliestactivetimeBean);
			getearliestactivetimeCall.callService();			
			Logger.verifyEquals("0", getearliestactivetimeCall.getString("code"), "验证code");
			ja = getearliestactivetimeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(getearliestactivetimeBean.getUserIds().size(), ja.size(), "验证记录条数");
			Logger.verifyEquals(activeTime, getearliestactivetimeCall.getJsonObject().get("data").getAsJsonArray().get(0).getAsJsonObject().get("activeTime").getAsString(), "验证activeTime是第一次加的时间");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}